package com.shoulder.ssh.utils;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.shoulder.ssh.properties.SSHProperties;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Properties;
import java.util.UUID;

@Data
public class SSHUtils {

    private SSHProperties properties;


    public Session init(String ip, int port, String username, String password) throws JSchException {

        JSch jsch = new JSch();
        Session session = jsch.getSession(username, ip, port);
        session.setPassword(password);
        Properties sshConfig = new Properties();
        sshConfig.put("StrictHostKeyChecking", properties.getStrictHostKeyChecking());
        session.setConfig(sshConfig);
        session.connect(properties.getTimeout());
        return session;
    }


    /**
     * 使用默认的端口初始化 ssh 连接信息
     *
     * @param ip       主机ip
     * @param username 用户名
     * @param password 密码
     */
    public Session init(String ip, String username, String password) throws JSchException {
        return init(ip, 22, username, password);
    }
}
